package com.nus.parsers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;

import com.nus.R;
import com.nus.representations.ShuttleMapLocations;

public class LocationAPI extends Activity {
	private static StringBuilder total = new StringBuilder();
	public static ArrayList<ShuttleMapLocations> map_t;
	public static String Latitude_item;
	public static String Longitude_item;
	public static String Loc_title;
	public LocationAPI(String locationtitle) {	 
	    // if file the available for reading
	  try{
		  Resources res = getResources();
		  InputStream instream = res.openRawResource(R.raw.building);
		  if (instream != null) {
	      // prepare the file for reading
	      InputStreamReader inputreader = new InputStreamReader(instream);
	      BufferedReader buffreader = new BufferedReader(inputreader);
	     

	      String line;
	 
	      // read every line of the file into the line-variable, on line at the time
	      while (( line = buffreader.readLine()) != null) {
	        // do something with the settings from the file
	    	  total.append(line);
	      }
	      instream.close();
	    }
		String response = total.toString();
		map_t = ParserAPIs.parseShuttleMapLocations(response);
		for (int i = 0; i<map_t.size(); i++) {
			if(locationtitle.equals(map_t.get(i).getTitle())){
				Latitude_item = map_t.get(i).getLatitude();
				Longitude_item = map_t.get(i).getLongitude();
			}
		}
		String uri = String
		.format("http://maps.google.com/maps/api/staticmap?center=" + Latitude_item + "," + Longitude_item + "&zoom=15&size=200x200&sensor=false");
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri
		.parse(uri));
		startActivity(intent);
	  }catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
